package com.sxx.beautifulcity.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.adapter.DeliciousFoodAdapter;
import com.sxx.beautifulcity.dialog.CommonProgressDialog;
import com.sxx.beautifulcity.entity.DeliciousFoodData;
import com.sxx.beautifulcity.entity.PetData;
import com.sxx.beautifulcity.entity.SpecialityData;
import com.sxx.beautifulcity.entity.Tuan_list_Tongyong;
import com.sxx.beautifulcity.entity.greendao.AgritourismDaoEntity;
import com.sxx.beautifulcity.entity.greendao.AroundDaoEntity;
import com.sxx.beautifulcity.entity.greendao.HolidayDaoEntity;
import com.sxx.beautifulcity.entity.greendao.PluckDaoEntity;
import com.sxx.beautifulcity.entity.greendao.VillageDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;
import com.sxx.beautifulcity.constant.Constant;
import com.sxx.beautifulcity.popmenu.CommonPopMenu;
import com.sxx.beautifulcity.utils.VolleySingleton;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/23.
 * 美食
 */
public class DeliciousFoodActivity extends DialogBaseActivity
        implements View.OnClickListener, OnMyItemClickListener {

    private RecyclerView recyclerview_fine_food;
    private final static String TAG = "DeliciousFoodActivity";
    private DeliciousFoodAdapter adapter;
    private TextView titleTv , moreTv;
    private ImageView backIV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview_fine_food);
        initView();
        requestfood();
    }

    /****
     * 初始化组件
     */
    private void initView() {

        titleTv = (TextView) findViewById(R.id.titleTv);
        titleTv.setText("美食");
        backIV = (ImageView) findViewById(R.id.backIV);
        backIV.setOnClickListener(this);

        moreTv = (TextView) findViewById(R.id.moreTv);
        moreTv.setText("");

        //请求队列初始化
        VolleySingleton.getInstance().getQueue();
        //适配器初始化
        adapter = new DeliciousFoodAdapter(this);

        recyclerview_fine_food = (RecyclerView) findViewById(R.id.recyclerview_fine_food);
        GridLayoutManager manager = new GridLayoutManager(this , 1);
        recyclerview_fine_food.setLayoutManager(manager);
        recyclerview_fine_food.setAdapter(adapter);
        adapter.setOnMyItemClick(this);

    }

    /***
     * 网络请求
     */
    private void requestfood() {
        final Dialog dialog = CommonProgressDialog.createLoadingDialog(this , "Loding....");
        dialog.show();

        String path = Constant.FINEFOOD_URL;
        JsonObjectRequest request = new JsonObjectRequest(path, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("list_data")) {
                            try {
                                JSONArray list_data = response.getJSONArray("list_data");
                                List<DeliciousFoodData> foodDatas = new ArrayList<DeliciousFoodData>();
                                Gson gson = new Gson();
                                for (int i = 0; i < list_data.length(); i++) {
                                    String str = list_data.getString(i);
                                    DeliciousFoodData data = gson.fromJson(str , DeliciousFoodData.class);
                                    foodDatas.add(data);
                                }
                                    adapter.addData(foodDatas);
                                    dialog.dismiss();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request , TAG);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backIV:
                Intent intent = new Intent(this , MainActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.left_out, R.anim.right_into);
                break;
        }
    }

    @Override
    public void onMyItemClick(Tuan_list_Tongyong data) {

    }

    @Override
    public void onMyItemClick(AroundDaoEntity data) {

    }

    @Override
    public void onMyItemClick(HolidayDaoEntity data) {

    }

    @Override
    public void onMyItemClick(AgritourismDaoEntity data) {

    }


    @Override
    public void onMyItemClick(PluckDaoEntity data) {

    }

    @Override
    public void onMyItemClick(VillageDaoEntity data) {

    }



    @Override
    public void onMyItemClick(SpecialityData data) {

    }

    @Override
    public void onMyItemClick(DeliciousFoodData data) {
        Intent intent = new Intent(this , DeliciousFoodDetailActivity.class);
        intent.putExtra("id" , data.getId());
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.left_out, R.anim.right_into);
    }

    @Override
    public void onMyItemClick(PetData data) {

    }
}
